package com.zyzd.retrofit.base;

import android.app.Dialog;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;

import com.zyzd.commontools.dialog.TipDialog;
import com.zyzd.commontools.utils.ToastUtil;
import com.zyzd.retrofit.R;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by lzy on 2018/5/15 0015.
 * description ${Activity 基类}
 */
public abstract class BaseActivity extends AppCompatActivity {
    private Dialog mProgressDialog;

    private Unbinder mUnBinder;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        mUnBinder = ButterKnife.bind(this);
        initData();
        setListener();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
            mProgressDialog = null;
        }
        if (mUnBinder != null) {
            mUnBinder.unbind();
        }
    }

    protected abstract int getLayoutId();

    protected abstract void initData();

    protected void setListener() {
    }

    public void showProgressBar() {
        if (mProgressDialog == null) {
            mProgressDialog = new TipDialog.Builder(this)
                    .setIconType(TipDialog.Builder.ICON_TYPE_LOADING)
                    .setTipWord(getString(R.string.loading))
                    .create(false);
        }
        mProgressDialog.show();
    }

    public void hideProgressBar() {
        if (mProgressDialog == null) return;
        mProgressDialog.dismiss();
    }

    public void replaceFragment(@IdRes int containerViewId, Fragment fragment, String tag) {
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        transaction.replace(containerViewId, fragment, tag)
                .commit();
    }

    public void addFragment(@IdRes int containerViewId, Fragment fragment, String tag) {
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        transaction.addToBackStack(null)
                .replace(containerViewId, fragment, tag)
                .commit();
    }

    public void showToast(String msg) {
        ToastUtil.showToast(msg);
    }

    public void showToast(int msgId) {
        ToastUtil.showToast(msgId);
    }

    /**
     * 请求权限
     *
     * @param requestCode 请求码
     * @param permissions 需要请求的权限数组
     * @return 是否有需要动态请求的权限
     */
    protected void requestPermissions(int requestCode, String... permissions) {
        ActivityCompat.requestPermissions(this, checkPermissions(permissions), requestCode);
    }

    /**
     * 判断当前是否有未通过的权限
     *
     * @param permissions 需要申请的权限
     * @return 未通过的权限
     */
    protected String[] checkPermissions(String... permissions) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return null;

        ArrayList<String> unGrantedPerms = null;
        for (String perm : permissions) {
            if (ContextCompat.checkSelfPermission(this, perm) != PackageManager.PERMISSION_GRANTED) {
                if (unGrantedPerms == null) unGrantedPerms = new ArrayList<>();
                unGrantedPerms.add(perm);
            }
        }
        return unGrantedPerms == null ? null : unGrantedPerms.toArray(new String[unGrantedPerms.size()]);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        ArrayList<String> allowPerms = new ArrayList<>();
        ArrayList<String> refusePerms = new ArrayList<>();
        ArrayList<String> forbidPerms = new ArrayList<>();
        for (int i = 0; i < permissions.length; i++) {
            String permission = permissions[i];
            if (grantResults[i] != PackageManager.PERMISSION_DENIED) {
                allowPerms.add(permission);
            } else if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
                refusePerms.add(permission);
            } else {
                forbidPerms.add(permission);
            }
        }
        onRequestPermissionsResult(requestCode, allowPerms, refusePerms, forbidPerms);
    }

    /**
     * 动态请求权限结果
     *
     * @param requestCode  请求码
     * @param allowPerms   已同意的权限
     * @param noAllowPerms 拒绝可再次申请的权限
     * @param forbidPerms  永久拒绝的权限
     */
    protected void onRequestPermissionsResult(int requestCode, ArrayList<String> allowPerms, ArrayList<String> noAllowPerms, ArrayList<String> forbidPerms) {

    }
}
